package com.zcsy.commons.util;

import com.zcsy.commons.mail.MailSenderInfo;
import com.zcsy.commons.mail.SimpleMailSender;


public class MailUtil {
	/** 邮件发送类 **/
	private static SimpleMailSender sms = new SimpleMailSender();
	
	/** 参数 **/
	private static PropertiesUtil prop = new PropertiesUtil("config.properties");
	
	private static MailSenderInfo initMailInfo(String toAddress,String subject,String content){
		 MailSenderInfo mailInfo = new MailSenderInfo();
	     mailInfo.setMailServerHost(prop.getProperty("mailServer_host"));   
	     mailInfo.setMailServerPort(prop.getProperty("mailServer_port"));   
	     mailInfo.setValidate("true".equals(prop.getProperty("mailServer_validate")) ? true : false);   
	     mailInfo.setUserName(prop.getProperty("mailServer_userName"));   
	     mailInfo.setPassword(prop.getProperty("mailServer_pwd"));
	     mailInfo.setFromAddress(prop.getProperty("mailServer_fromAddr"));   
		 mailInfo.setToAddress(toAddress);   
	     mailInfo.setSubject(subject);   
	     mailInfo.setContent(content);   
	     return mailInfo;
	}
	
	/**
	 * 系统发送邮件，用于发送邮件内容为文本的邮件
	 * @param toAddress 收件人email地址
	 * @param subject 邮件标题
	 * @param text 邮件内容 文本
	 */
	public static void sendTextMail(String toAddress,String subject,String text){
		sms.sendTextMail(initMailInfo(toAddress,subject,text));
	}
	
	/**
	 * 系统发送邮件，用于发送邮件内容为html页面的邮件
	 * @param toAddress 收件人email地址
	 * @param subject  邮件标题
	 * @param html 邮件内容 html页面
	 */
	public static void sendHtmlMail(String toAddress,String subject,String html){
		sms.sendHtmlMail(initMailInfo(toAddress,subject,html));
	}
	
	/**
	 * 
	 * @Description <br>
	 * 批量发送邮件方法
	 * @param emails 邮箱数组
	 * @param content 文件正文
	 * @param subject 文件主题
	 * @return 发送结果
	 * @author chensf 735569870@qq.com
	 * @date 2016年1月11日
	 * @see
	 */
	public static boolean sendEmail(String[] emails,String content,String subject){
	    boolean sendResult = false;
	    MailUtils cn = new MailUtils();
        
        // 多用户，多附件，需要循环遍历
        for(String email : emails){
            cn.setHost("smtp.163.com");
            cn.setUserName("whfywh@163.com");
            cn.setPassWord("fy0828");
            cn.setTo(email);
            cn.setFrom("whfywh@163.com");
            cn.setSubject(subject);
            cn.setContent(content);
            sendResult = cn.sendMail();
        }
        
        return sendResult;
	}
	
}
